package br.com.linkin.mi.utils.security;

import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author daniel.mendonca
 */
public class Base64 {
    
    private BASE64Decoder decoder = new BASE64Decoder();
    private BASE64Encoder encoder = new BASE64Encoder();
    
    private static Base64 instance = new Base64();
    
    /**
     * Base64 instance
     * @return 
     */
    public static Base64 instance() {
        return instance;
    }
    
    /**
     * Encode to Base 64
     * @param valor
     * @return
     * @throws IOException 
     */
    public static String encode(String valor) throws IOException{
        return Base64.instance().encodeMethod(valor);
    }
    
    /**
     * Decode from Base 64
     * @param valor
     * @return
     * @throws IOException 
     */
    public static String decode(String valor) throws IOException{
        return Base64.instance().decodeMethod(valor);
    }
    
    /**
     * encode Method
     * @param valor
     * @return 
     */
    private String encodeMethod(String valor){
        return encoder.encodeBuffer(valor.getBytes());
    }
    
    /**
     * decode Method
     * @param valor
     * @return 
     */
    private String decodeMethod(String valor) throws IOException{
        return new String( decoder.decodeBuffer(valor) );
    }
    
}
